package com.example.wechat_applet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wechat_applet.mapper.UserMapper;
import com.example.wechat_applet.pojo.LoginUser;
import com.example.wechat_applet.pojo.Role;
import com.example.wechat_applet.pojo.User;
import com.example.wechat_applet.service.RoleService;
import com.example.wechat_applet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    @Autowired
    private UserService userService;
    
    @Autowired
    UserMapper userMapper;

//    @Autowired
//    private MenuDao menuDao;
	
	@Autowired
	private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
        
        //查询用户
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getName,username));
        //用户不能为空
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }

//        //查询用户权限
//        //List<String> roles = new ArrayList<>(Arrays.asList("test"));
//        List<String> roles = menuDao.selectPermsByUserId(user.getId());

		//List<Role> rolesByUserId = roleService.list(new LambdaQueryWrapper<Role>().eq(Role::get,user.getId()));//user.getId()

//		System.out.println(rolesByUserId);
//		// 将用户权限填充进去
//		user.setRoleList(rolesByUserId);
//
//		System.out.println(user);

//        LoginUser loginUser = new LoginUser(user);
//        //搜索该用户具有的角色
        List<Role> roleNames = userMapper.haveRole(user.getId());
//        loginUser.setRoleList(roleNames);

        return new LoginUser(user,roleNames);
    }
}
//*/